package com.hc.model.utils;

import java.util.ArrayList;

import com.hc.DTO.AnimalCategoryMasterDTO;
import com.hc.DTO.AnimalDTO;
import com.hc.DTO.AnimalImagesDTO;
import com.hc.common.ActionEvent;
import com.hc.constants.Constants;
import com.hc.controller.AnimalImagesController;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;

public class AnimalImagesModel {
	public static AnimalImagesModel instance;

	public static AnimalImagesModel getInstance() {
		if (instance == null)
			instance = new AnimalImagesModel();
		return instance;
	}

	public void handleControllerEvent(ActionEvent e) {

		switch (e.action) {
		case Constants.GET_ALL_IMAGES_ANIMAL:
			ArrayList<AnimalImagesDTO> arrData = getAllImages((Context) e.sender);
			e.viewData = arrData;
			AnimalImagesController.getInstance().handleModelViewEvent(e);
			break;

		case Constants.GALLERY:
			Bundle data = (Bundle) e.bundle;
			int id = data.getInt("imagesgallary");
			ArrayList<AnimalImagesDTO> arrDataImages = getAllImagesById(
					(Context) e.sender, id);
			e.viewData = arrDataImages;
			AnimalImagesController.getInstance().handleModelViewEvent(e);
			break;

		case Constants.GET_ANIMAL_BY_CATEGORY:
			Bundle category = (Bundle) e.bundle;
			int idCategory = category.getInt("idAnimal");
			ArrayList<AnimalImagesDTO> arrImages = GetAnimalByCategory(
					(Context) e.sender, idCategory);
			e.viewData = arrImages;
			AnimalImagesController.getInstance().handleModelViewEvent(e);
			break;

		default:
			break;
		}
	}

	ArrayList<AnimalImagesDTO> getAllImages(Context context) {
		ArrayList<AnimalImagesDTO> arr = new ArrayList<AnimalImagesDTO>();
		String sql = "SELECT * FROM animalimages";
		DBHelper db = new DBHelper(context);
		Cursor c = db.Excutequery(sql);
		while (c.moveToNext()) {
			AnimalImagesDTO dto = new AnimalImagesDTO();
			dto.setId(c.getInt(c.getColumnIndex("Id")));
			dto.setAnimalId(c.getInt(c.getColumnIndex("AnimalId")));
			dto.setImageUrl(c.getString(c.getColumnIndex("ImageUrl")));
			dto.setActualName(c.getString(c.getColumnIndex("ActualName")));
			dto.setDescription(c.getString(c.getColumnIndex("Description")));
			arr.add(dto);
		}
		c.close();
		db.close();

		return arr;
	}

	ArrayList<AnimalImagesDTO> getAllImagesById(Context context, int id) {
		ArrayList<AnimalImagesDTO> arr = new ArrayList<AnimalImagesDTO>();
		String sql = "SELECT * FROM animalimages where AnimalId = " + id;
		DBHelper db = new DBHelper(context);
		Cursor c = db.Excutequery(sql);
		while (c.moveToNext()) {
			AnimalImagesDTO dto = new AnimalImagesDTO();
			dto.setId(c.getInt(c.getColumnIndex("Id")));
			dto.setAnimalId(c.getInt(c.getColumnIndex("AnimalId")));
			dto.setImageUrl(c.getString(c.getColumnIndex("ImageUrl")));
			dto.setActualName(c.getString(c.getColumnIndex("ActualName")));
			dto.setDescription(c.getString(c.getColumnIndex("Description")));
			arr.add(dto);
		}
		c.close();
		db.close();
		return arr;
	}

	ArrayList<AnimalImagesDTO> GetAnimalByCategory(Context context, int id) {
		ArrayList<AnimalImagesDTO> arr = new ArrayList<AnimalImagesDTO>();
		String sql = "SELECT * FROM animalimages img, animal a where img.AnimalId = a.IdAnimal and a.IdAnimal = "
				+ id;
		DBHelper db = new DBHelper(context);
		Cursor c = db.Excutequery(sql);
		while (c.moveToNext()) {
			AnimalImagesDTO dto = new AnimalImagesDTO();
			dto.setId(c.getInt(c.getColumnIndex("Id")));
			dto.setAnimalId(c.getInt(c.getColumnIndex("AnimalId")));
			dto.setImageUrl(c.getString(c.getColumnIndex("ImageUrl")));
			dto.setActualName(c.getString(c.getColumnIndex("ActualName")));
			dto.setDescription(c.getString(c.getColumnIndex("Description")));
			arr.add(dto);
		}
		c.close();
		db.close();
		return arr;
	}

	public ArrayList<String> GallaryById(Context context, int id) {
		ArrayList<String> arr = new ArrayList<String>();
		String sql = "SELECT * FROM animalimages where AnimalId = " + id;
		DBHelper db = new DBHelper(context);
		Cursor c = db.Excutequery(sql);
		while (c.moveToNext()) {
			arr.add(c.getString(c.getColumnIndex("ImageUrl")));
		}
		c.close();
		db.close();
		return arr;
	}

	public boolean getIdImges(Context context, int id) {
		boolean flag = false;
		String sql = "SELECT * FROM animalimages where Id=" + id;
		DBHelper db = new DBHelper(context);
		Cursor c = db.Excutequery(sql);
		while (c.moveToNext()) {
			flag = true;
			break;
		}
		c.close();
		db.close();
		return flag;
	}

	public boolean insertImages(AnimalImagesDTO dto, Context context) {
		boolean flag = false;
		String DATABASE_TABLE = "animalimages";
		ContentValues values = new ContentValues();
		values.put("Id", dto.getId());
		values.put("AnimalId", dto.getAnimalId());
		values.put("ImageUrl", dto.getImageUrl());
		values.put("ImageName", dto.getImageName());
		values.put("ActualName", dto.getActualName());
		values.put("Description", dto.getDescription());
		values.put("IsCover", dto.isCover());

		DBHelper db = DBHelper.getInstance(context);
		flag = db.ExcuteInsert(null, DATABASE_TABLE, values);
		db.close();

		return flag;
	}

	public boolean updateImamges(AnimalImagesDTO dto, Context context) {
		boolean flag = false;
		// String sql = "UPDATE changelog SET Id=" + id;
		DBHelper db = new DBHelper(context);
		String where = "Id=" + dto.getId() + "";
		ContentValues values = new ContentValues();
		values.put("AnimalId", dto.getAnimalId());
		values.put("ImageUrl", dto.getImageUrl());
		values.put("ImageName", dto.getImageName());
		values.put("ActualName", dto.getActualName());
		values.put("Description", dto.getDescription());
		values.put("IsCover", dto.isCover());
		flag = db.ExcuteUpdate(null, "animalimages", values, where, null);
		db.close();
		return flag;
	}

	public void deleteAnimalIMG(AnimalImagesDTO animal, Context context) {
		String sql = "Id=" + animal.getIdImgeDelete();
		DBHelper db = DBHelper.getInstance(context);
		String DATABASE_TABLE = "animalimages";
		db.deleteTable(DATABASE_TABLE, sql);
		db.close();
	}
}
